package com.tanhua.server.controller;

import com.alibaba.fastjson.JSON;
import com.tanhua.model.db.Answers;
import com.tanhua.model.db.Questionnaire;
import com.tanhua.model.db.Report;
import com.tanhua.server.service.TestSoulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /*
    查询问卷
     */
    @GetMapping
    public ResponseEntity testSoul() {
        List<Questionnaire> list = testSoulService.findQuestionnaire();
        Object[] objects = list.toArray();
        return ResponseEntity.ok(objects);
    }

    /*
    问卷提交/testSoul
     */
    @PostMapping
    public ResponseEntity testSoul(@RequestBody Map map) {
        String answers1 = JSON.toJSONString(map.get("answers"));
        List<Answers> answers = JSON.parseArray(answers1, Answers.class);
       String reportId= testSoulService.statistics(answers);
        return ResponseEntity.ok(reportId);
    }

    /*
    /testSoul/report/:id
    报告查询
     */
    @GetMapping("/report/{id}")
    public ResponseEntity report(@PathVariable("id") Integer reportId) {
        Report report = testSoulService.findReport(reportId);
        return ResponseEntity.ok(report);
    }
}
